<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

// 导入 Intervention Image Manager Class
use Intervention\Image\ImageManagerStatic as Image;

class FrilinkController extends Controller
{
    //frilink友情链接
    public function frilink()
    {
    	$data = DB::table('fri_link') -> select('fri_link_id','title','description','url','pic','disabled') -> paginate(5);
    	// dd($data);
    	return view('admin/website/frilink/frilink',['data'=>$data]);
    }

    //add添加友情链接
    public function add()
    {
    	return view('admin/website/frilink/add');
    }

    //添加数据到数据库
    public function insert(Request $request)
    {
    	$data = $request -> except('_token');

    	//验证
    	$this -> validate($request,[
                'title' => 'required',
                'description' => 'required',
                'pic' => 'required',

            ],[
                'title.required' => '友情链接名称不能为空',
                'description.required' => '友情链接描述不能为空',
                'pic.required' => '链接图片不能为空',
            ]);
    	// dd($data);
    	//处理图片
        if($request -> hasFile('pic'))
        {
            if($request -> file('pic') -> isValid())
            {
                //获取图片的后缀名
                $suffix = $request -> file('pic') -> getClientOriginalExtension();
                // echo $suffix;
                
                //随即一个文件名
                $fileName = time().mt_rand(100000,999999).'.'.$suffix;


                // $path = date('Ymd');
                $path = './uploads/website/frilink';
                // $fname = $path . '/' . $fileName;
                
                $request -> file('pic') -> move($path,$fileName);
                $size = getimagesize($path.'/'.$fileName);
                
                //获取想要缩放的比例
                $w = 300;
                $h = 300;
                if($w/$size[0] > $h/$size[1])
                {
                    $bili = $h/$size[1];
                }else{
                    $bili = $w/$size[0];
                }
                
                //最终的比例
                $nw = floor($size[0]*$bili);
                $nh = floor($size[1]*$bili);
                

                $data['pic'] = $fileName;
                // dd($data);
                $fname = $path . '/' . $fileName;
                $img = Image::make($fname)->resize($nw,$nh)->save($fname);
                // echo $data['pic'];
            }
        }
        // dd($data);
        $res = DB::table('fri_link') -> insert($data);
        // dd($res);
        if($res)
        {
        	return redirect('admin/frilink') -> with('success','添加成功');
        }else{
        	return back() -> with('error','添加失败');
        }

    	
    }

    //edit编辑友情链接
    public function edit($id)
    {
    	$data = DB::table('fri_link') -> where('fri_link_id','=',$id) -> first();

    	return view('admin/website/frilink/edit',['data'=>$data]);
    }

    //update更新友情链接
    public function update(Request $request)
    {

    	$data = $request -> except('_token','old_pic');
        //$data = $request -> all();
    	// dd($data);
        // dd($request->old_pic);
    	$this -> validate($request,[
                'title' => 'required',
                'description' => 'required',
                

            ],[
                'title.required' => '友情链接名称不能为空',
                'description.required' => '友情链接描述不能为空',
                
            ]);
    	//处理图片
    	if($request -> hasFile('pic'))
        {
            if($request -> file('pic') -> isValid())
            {
                //获取图片的后缀名
                $suffix = $request -> file('pic') -> getClientOriginalExtension();
                // echo $suffix;
                
                //随即一个文件名
                $fileName = time().mt_rand(100000,999999).'.'.$suffix;


                // $path = date('Ymd');
                $path = './uploads/website/frilink';
                // $fname = $path . '/' . $fileName;
                
                $request -> file('pic') -> move($path,$fileName);
                $size = getimagesize($path.'/'.$fileName);
                
                //获取想要缩放的比例
                $w = 300;
                $h = 300;
                if($w/$size[0] > $h/$size[1])
                {
                    $bili = $h/$size[1];
                }else{
                    $bili = $w/$size[0];
                }
                
                //最终的比例
                $nw = floor($size[0]*$bili);
                $nh = floor($size[1]*$bili);
                

                $data['pic'] = $fileName;
                
                $fname = $path . '/' . $fileName;

                $img = Image::make($fname)->resize($nw,$nh)->save($fname);
                if(file_exists('./uploads/website/frilink/'.$request->old_pic)){
                    unlink('./uploads/website/frilink/'.$request->old_pic);
                }
            }
        }
    	$res = DB::table('fri_link') -> where('fri_link_id','=',$data['fri_link_id']) -> update($data);
    	
    	if($res)
    	{
    		return redirect('admin/frilink') -> with('success','更新成功');
    	}else{
    		return back() -> with('error','更新失败');
    	}
    }

    //修改状态 status
    public function status($id)
    {
		//更新状态
		$disabled = DB::table('fri_link') -> where('fri_link_id','=',$id) -> first() -> disabled;
		if($disabled == 1)
		{
			$res = DB::table('fri_link') -> where('fri_link_id','=',$id) -> update(['disabled'=>0]);
		}else{
			$res = DB::table('fri_link') -> where('fri_link_id','=',$id) -> update(['disabled'=> 1]);
		}
		return redirect('admin/frilink');
    }


   	//delete删除友情链接
   	public function delete($id)
   	{
   		$allid = DB::table('fri_link') ->select('fri_link_id') -> get();
   		$arr = [];
   		foreach($allid as $key=> $val)
   		{
   			$arr[] = $val -> fri_link_id;
   		}
   		// dd($arr);
   		if(in_array($id,$arr))
   		{

   			$res = DB::table('fri_link') -> where('fri_link_id','=',$id) -> delete();
	   		if($res)
	   		{
	   			return redirect('admin/frilink') -> with(['success'=>'删除成功']);
	   		}else{
	   			return back() -> with(['error'=>'删除失败']);
	   		}
   		}else{
   			
   			return back() -> with(['error'=>'没有该数据']);
   		}
   		
   	}
}
